﻿using MediatR;
using SmartCity.ProcessEngine.API.Domain;
using SmartCity.ProcessEngine.API.ErrorCode;
using SmartCity.ProcessEngine.API.Infrastructrue.Extensions;
using SmartCity.ProcessEngine.API.Infrastructrue.Interfaces;
using SmartCity.ProcessEngine.API.Write.Applications.Commands;

namespace SmartCity.ProcessEngine.API.Write.Applications.CommandHandlers
{
    public class FormTypeUpdateCommandHandler : IRequestHandler<FormTypeUpdateCommand, APIResult<int>>
    {
        private readonly IBaseRespository<WF_FromType> typeRep;

        public FormTypeUpdateCommandHandler(IBaseRespository<WF_FromType> typeRep)
        {
            this.typeRep = typeRep;
        }

        public async Task<APIResult<int>> Handle(FormTypeUpdateCommand request, CancellationToken cancellationToken)
        {
            APIResult<int> result=new APIResult<int>();
            result.Code=APIResultCode.UpdateSuccess;
            result.Message=APIResultCode.UpdateSuccess.GetDescription();

            var res = typeRep.GetAll().FirstOrDefault(x => x.TypeId == request.TypeId);
            res.TypeName=request.TypeName;
            res.IndustryId=request.IndustryId;  
            await typeRep.UpdateAsync(res);

            return await Task.FromResult(result);
        }
    }
}
